<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--站位数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="站位编号" prop="stationId">
            <el-input
              v-model="queryParams.stationId"
              placeholder="请输入站位编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="站位名称" prop="stationName">
            <el-input
              v-model="queryParams.stationName"
              placeholder="请输入站位名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="企业状态" prop="stationStatus">
            <el-select v-model="queryParams.stationStatus" placeholder="请选择企业状态" clearable size="small">
              <el-option
                v-for="dict in stationStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['demo:station:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['demo:station:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['demo:station:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['demo:station:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <!--<el-table-column label="主键自增长" align="center" prop="id" />-->
          <el-table-column label="编号" align="center" prop="stationId"/>
          <el-table-column label="名称" align="center" prop="stationName"/>
          <el-table-column label="IP" align="center" prop="stationIp"/>
          <el-table-column label="地址" align="center" prop="stationAddr" :show-overflow-tooltip="true"/>
          <el-table-column label="行政区划" align="center" prop="areaName"/>
          <!--<el-table-column label="行政区划编码" align="center" prop="areaCode" />-->
          <el-table-column label="行业代码" align="center" prop="tradeCode"/>
          <!--<el-table-column label="法人姓名" align="center" prop="deputy" />
          <el-table-column label="法人电话" align="center" prop="deputyPhone" />
          <el-table-column label="工商注册码" align="center" prop="regCode" />
          <el-table-column label="法人代码" align="center" prop="epCode" />
          <el-table-column label="联系人" align="center" prop="linkMan" />
          <el-table-column label="联系电话" align="center" prop="linkPhone" />-->
          <el-table-column label="企业状态" align="center" prop="stationStatus" :formatter="stationStatusFormat"/>
          <!--<el-table-column label="经度" align="center" prop="longitude" />
          <el-table-column label="纬度" align="center" prop="latitude" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['demo:station:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['demo:station:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改站位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" style="padding: 0 50px;">
        <el-row>
          <el-col :span="12">
            <el-form-item label="站位编号" prop="stationId">
              <el-input v-model="form.stationId" placeholder="请输入站位编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站位名称" prop="stationName">
              <el-input v-model="form.stationName" placeholder="请输入站位名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="站位地址" prop="stationAddr">
              <el-input v-model="form.stationAddr" placeholder="请输入站位地址"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="站位IP" prop="stationIp">
              <el-input v-model="form.stationIp" placeholder="请输入站位IP"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业代码" prop="tradeCode">
              <el-input v-model="form.tradeCode" placeholder="请输入行业代码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="areaName">
              <el-input v-model="form.areaName" placeholder="请输入行政区划"/>
            </el-form-item>
            <!--<el-form-item label="行政区划编码" prop="areaCode">
              <el-input v-model="form.areaCode" placeholder="请输入行政区划编码" />
            </el-form-item>-->
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="法人姓名" prop="deputy">
              <el-input v-model="form.deputy" placeholder="请输入法人姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人电话" prop="deputyPhone">
              <el-input v-model="form.deputyPhone" placeholder="请输入法人电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工商注册码" prop="regCode">
              <el-input v-model="form.regCode" placeholder="请输入工商注册码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人代码" prop="epCode">
              <el-input v-model="form.epCode" placeholder="请输入法人代码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkMan">
              <el-input v-model="form.linkMan" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkPhone">
              <el-input v-model="form.linkPhone" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="企业状态" prop="stationStatus">
          <!--<el-select v-model="form.stationStatus" placeholder="请选择企业状态">
            <el-option
              v-for="dict in stationStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>-->
          <el-radio v-model="form.stationStatus" :label="0">正常</el-radio>
          <el-radio v-model="form.stationStatus" :label="1">停产</el-radio>
          <el-radio v-model="form.stationStatus" :label="2">关闭</el-radio>
          <!--<el-radio-group v-model="form.stationStatus">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">停产</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>-->
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listStation, getStation, delStation, addStation, updateStation, exportStation} from "@/api/demo/station";
  import { treeselect } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Station",
    components: { Treeselect },
    data() {
      return {
        // 部门树选项
        deptOptions: undefined,
        // 部门名称
        deptName: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },

        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 站位表格数据
        stationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 企业状态字典
        stationStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stationId: null,
          stationName: null,
          stationStatus: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          stationId: [
            {required: true, message: "站位编号不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      console.log(this.form.stationStatus)
      this.getList();
      this.getTreeselect();
      this.getDicts("station_status").then(response => {
        this.stationStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询站位列表 */
      getList() {
        this.loading = true;
        listStation(this.queryParams).then(response => {
          this.stationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.getList();
      },
      // 企业状态字典翻译
      stationStatusFormat(row, column) {
        return this.selectDictLabel(this.stationStatusOptions, row.stationStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          stationId: null,
          stationName: null,
          stationIp: null,
          stationAddr: null,
          areaName: null,
          areaCode: null,
          tradeCode: null,
          deputy: null,
          deputyPhone: null,
          regCode: null,
          epCode: null,
          linkMan: null,
          linkPhone: null,
          stationStatus: 0,
          longitude: null,
          latitude: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加站位";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStation(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改站位";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStation(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addStation(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除站位编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delStation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有站位数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportStation(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
